﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GuestBook_Lib
{
    public class Signature
    {
        internal Signature()
        {
            //Local Assembalies Only: Array Declarations
            Location = "";
            Team = 0;
            Time = "";
        }
        internal Signature(string name , string time)
        {
            Name = name;
            Time = time;
            Team = 0;
            Location = "";
        }
        internal Signature(string name, string time, int team)
        {
            Name = name;
            Time = time;
            Team = team;
            Location = "";
        }
        internal Signature(string name, string time, int team, string location)
        {
            Name = name;
            Time = time;
            Team = team;
            Location = location;
        }
        public Signature(string name)
        {
            //string i = DateTime.Now.ToShortTimeString();
            //MessageBox.Show(i);
            Name = name;
            Time = DateTime.Now.ToShortTimeString() +  " on " + DateTime.Now.ToShortDateString();
            Location = "";
            Team = 0;
        }
        public Signature(string name, int team)
        {
            Location = "";
            Name = name;
            Team = team;
            Time = DateTime.Now.ToShortTimeString() + " on " + DateTime.Now.ToShortDateString();
        }
        public Signature(string name, int team, string location)
        {
            Team = team;
            Location = location;
            Name = name;
            Time = DateTime.Now.ToShortTimeString() + " on " + DateTime.Now.ToShortDateString();
        }
        public string GetWritable()
        {
            string w = "";

            
            if (Name != "")
            {
                w += Name + " signed at: ";
            }            
            if (Time == "")
            {
                return w;
            }
            w += Time;
            if (Team != 0)
            {
                w += "; They are on team: " + Team.ToString();
            }
            if (Location != "")
            {
                w += " ,from " + Location;
            }
            w += "!";
            return w;
        }
        public string name()
        {
            return Name;
        }
        internal void Destroy()
        {
            System.IO.File.AppendAllText(@"C://GuestbookLogs.txt", DateTime.Now.ToShortDateString() + " , " + DateTime.Now.ToShortTimeString() + "- " + Name + " was removed.");
            Name = "";
            Team = 0;
            Location = "";
            Time = "";
        }
        protected string Name;
        protected int Team;
        protected string Location;
        protected string Time;
    }
    public class Book : Signature
    {
        public Book()
        {
            //sign = new Signature[100];
            //G_Chck();
            sign = new Signature[1]; //Minimum = 1, Max Pos : 0
        }
        public Book(int goal)
        {
            Goal = goal;
            sign = new Signature[1];
        }
        public void Write(string target)
        {
            
            for (int i = 0; i < sign.Length; i++)
            {
                string c = sign[i].GetWritable();
                try
                {
                    System.IO.File.AppendAllText(target, c);
                }
                catch (Exception e)
                {
                    System.IO.File.AppendAllText(@"C://GuestbookError.txt", e.ToString());
                }
                finally
                {
                    //To do: Do I need anything to occur afterwards?
                }
            }
        }
        protected void SaveTstr(string i)
        {
            if (i != "s" && i != "")
            {
                if (sign.Length > 0)
                {
                    try
                    {
                        string[] TMP = new string[sign.Length];
                        for (int o = 0; o < sign.Length; o++)
                        {
                            TMP[o] = sign[o].GetWritable();
                        }

                        System.IO.File.WriteAllLines(i, TMP);
                    }
                    catch (Exception EX)
                    {
                        MessageBox.Show(EX.Message);
                    }
                    //System.IO.File.WriteAllText(i, "Smiles");
                }
            }
        }
        public void Save_to_String_File()
        {
            string i = "s";
            SaveFileDialog s = new SaveFileDialog();
            s.DefaultExt = ".txt";
            s.Filter = "Book Files: *.book| *.book|Text Files: *.txt| *.txt";
            s.FilterIndex = 2;
            s.ShowDialog();
            i = s.FileName;
            SaveTstr(i);
        }
        public void File_To_Array(string path)
        {
            //Header: <# of Arguments> Min: 2, name , time
            //Arguments
            //Next Header...
            //Example: 2 (Name only)
            //My name
            //Time sign
            //return 
        }
        private string PathS()
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Text Files: *.txt | *.txt";
            string r = "o";
            o.ShowDialog();
            r = o.FileName;
            if (r != "o")
            {
                return r;
            }
            MessageBox.Show("Warning: No path selected, file saved to Application Folder.");
            return Application.StartupPath + "Temp.txt";
        }
        public string [] Load_String_Book()
        {
            string path = PathS();
            string[] array = System.IO.File.ReadAllLines(path);
            return array;
        }
        public void Load_String_Book(ListBox Tmp)
        {

            string path = PathS();
            if (path != "")
            {
                string[] array = System.IO.File.ReadAllLines(path);
                Tmp.Items.Clear();
                Tmp.Items.AddRange(array);
            }
            else
            {

            }
        }
        //int OldPos = 0;
        protected int Goal = -1;
        protected Signature[] sign;
        public string AddSign(string name)
        {
            //sign = new Signature[10];

            //string i = DateTime.Now.ToShortTimeString();
            //MessageBox.Show(i);
            int s = sign.Length + 1;
            Signature sig = new Signature(name);
            if (sign.Length > 1 || frst == true)
            {
                Signature[] t = new Signature[s];
                sign.CopyTo(t, 0);
                t[t.Length - 1] = sig;
                sign = t;
                frst = false;
            }
            else
            {
                sign[0] = sig;
                frst = true;
            }            
            G_Chck(name);
            return sign[sign.Length - 1].GetWritable();
        }
        private bool frst = false;
        public string AddSign(string name, int team)
        {
            
            int s = sign.Length + 1;
            Signature sig = new Signature(name , team);
            if (sign.Length > 1 || frst == true)
            {
                Signature[] t = new Signature[s];
                sign.CopyTo(t, 0);
                t[t.Length - 1] = sig;
                sign = t;
                frst = false;
            }
            else
            {
                sign[0] = sig;
                frst = true;
            }
            G_Chck(name);
            return sign[sign.Length - 1].GetWritable();
        }
        public string AddSign(string name, int team, string location)
        {
            
            int s = sign.Length + 1;
            Signature sig = new Signature(name , team , location);
            if (sign.Length > 1 || frst == true)
            {
                Signature[] t = new Signature[s];
                sign.CopyTo(t, 0);
                t[t.Length - 1] = sig;
                sign = t;
                frst = false;
            }
            else
            {
                sign[0] = sig;
                frst = true;
            }
            G_Chck(name);
            return sign[sign.Length - 1].GetWritable();
        }
        public bool Contains(string name , bool destroy)
        {
            for (int i = 0; i < sign.Length; i++)
            {
                if (sign[i].name() == name)
                {
                    if (destroy == true)
                    {
                        sign[i].Destroy();
                    }
                    return true;
                }
            }
            return false;
        }
        public void Set_Goal(int val)
        {
            Goal = val;
        }
        public void Set_Goal(long val)
        {
            Goal = Convert.ToInt32(val);
        }
        private void G_Chck(string nme)
        {
            if (sign.Length == Goal)
            {
                MessageBox.Show("Thank you, " + nme + ", we just made our goal in Signatures!");
            }
            else
            { //Danger: Length - 1 isn't initialized; It can fatally crash!
                string messge = "Thank you for signing, Mr. or Mrs. " + nme + ", you are the ";                    
                if (sign.Length == 1)
                {
                    messge += "1st person";
                }
                else if (sign.Length == 2)
                {
                    messge += "2nd person";
                }
                else if (sign.Length == 3)
                {
                    messge += "3rd person";
                }
                else
                {
                    messge += sign.Length.ToString() + "th person";
                }                
                messge += "!";
                //int temp = 13;
                MessageBox.Show(messge);
            }
        }
    }

}
